{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}
{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/datepicker/datepicker3.css' %}" rel="stylesheet">
    <style>
        .ul-padding-0 {
            padding-inline-start: 0px
        }
        .ul-padding-20 {
            padding-inline-start: 20px
        }
        ul li{
            list-style-type: none;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{{ action }}</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <form method="post" class="form-horizontal" action="" >
                            {% if form.non_field_errors %}
                                 <div class="alert alert-danger">
                                     {{ form.non_field_errors }}
                                 </div>
                            {% endif %}
                            {% csrf_token %}

                            <h3>{% trans 'Basic' %}</h3>
                            {% bootstrap_field form.name layout="horizontal" %}

                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'User' %}</h3>
                            {% bootstrap_field form.users layout="horizontal" %}
                            {% bootstrap_field form.user_groups layout="horizontal" %}

                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'Asset' %}</h3>
                            {% bootstrap_field form.assets layout="horizontal" %}
                            {% bootstrap_field form.nodes layout="horizontal" %}
                            {% bootstrap_field form.system_users layout="horizontal" %}

                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'Action' %}</h3>
                            <div class="form-group required">
                                <label class="col-md-2 control-label">{{ form.actions.label }}</label>
                                <div class="col-md-9">
                                    <div id="id_actions">
                                        <ul class="ul-padding-0">
                                            <li>
                                                <div class="checkbox">{{ form.actions.0}}</div>
                                                <ul class="ul-padding-20">
                                                    <li>
                                                        <div class="checkbox">{{ form.actions.1}}</div>
                                                    </li>

                                                    <li>
                                                        <div class="checkbox">{{ form.actions.4}}</div>
                                                        <ul class="ul-padding-20">
                                                            <li>
                                                                <div class="checkbox">{{ form.actions.2}}</div>
                                                            </li>
                                                            <li>
                                                                <div class="checkbox">{{ form.actions.3}}</div>
                                                            </li>
                                                        </ul>
                                                    </li>
                                                </ul>

                                            </li>
                                        </ul>
                                        <div class="help-block">{{ form.actions.help_text }}</div>
                                    </div>
                                </div>
                            </div>

                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'Other' %}</h3>
                            <div class="form-group">
                                <label for="{{ form.is_active.id_for_label }}" class="col-sm-2 control-label">{% trans 'Active' %}</label>
                                <div class="col-sm-8">
                                    {{ form.is_active }}
                                </div>
                            </div>
                            <div class="form-group {% if form.date_expired.errors or form.date_start.errors %} has-error {% endif %}" id="date_5">
                                <label for="{{ form.date_expired.id_for_label }}" class="col-sm-2 control-label">{% trans 'Validity period' %}</label>
                                <div class="col-sm-9">
                                    <div class="input-daterange input-group" id="datepicker">
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        {% if form.errors %}
                                            <input type="text" class="input-sm form-control" id="date_start" name="date_start" value="{{ form.date_start.value }}">
                                            <span class="input-group-addon">to</span>
                                            <input type="text" class="input-sm form-control" id="date_expired" name="date_expired" value="{{ form.date_expired.value }}">
                                        {% else %}
                                            <input type="text" class="input-sm form-control" id="date_start" name="date_start" value="{{ form.date_start.value|date:'Y-m-d H:i' }}">
                                            <span class="input-group-addon">to</span>
                                            <input type="text" class="input-sm form-control" id="date_expired" name="date_expired" value="{{ form.date_expired.value|date:'Y-m-d H:i' }}">
                                        {% endif %}
                                    </div>
                                    <span class="help-block ">{{ form.date_expired.errors }}</span>
                                    <span class="help-block ">{{ form.date_start.errors }}</span>
                                </div>
                             </div>
                            {% bootstrap_field form.comment layout="horizontal" %}

                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
                                    <button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
                                </div>
                            </div>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% include 'assets/_asset_list_modal.html' %}
{% endblock %}
{% block custom_foot_js %}
<script src="{% static 'js/plugins/datepicker/bootstrap-datepicker.js' %}"></script>
<script type="text/javascript" src='{% static "js/plugins/daterangepicker/moment.min.js" %}'></script>
<script type="text/javascript" src='{% static "js/plugins/daterangepicker/daterangepicker.min.js" %}'></script>
<link rel="stylesheet" type="text/css" href={% static "css/plugins/daterangepicker/daterangepicker.css" %} />

<script>
var api_action =  "{{ api_action }}";

var idActionAll = "id_actions_0";
var idActionConnect = "id_actions_1";
var idActionUpload = "id_actions_2";
var idActionDownload = "id_actions_3";
var idActionUpDownload = "id_actions_4";
var elementActionAll = document.getElementById(idActionAll);
var elementActionConnect = document.getElementById(idActionConnect);
var elementActionUpload = document.getElementById(idActionUpload);
var elementActionDownload = document.getElementById(idActionDownload);
var elementActionUpDownload = document.getElementById(idActionUpDownload);
var elementActionUpDownloadChildren = [elementActionUpload, elementActionDownload];
var elementActionAllChildren = [
    elementActionConnect, elementActionUpload, elementActionDownload, elementActionUpDownload
];

$(document).ready(function () {
    $('.select2').select2({
        closeOnSelect: false
    });
    nodesSelect2Init(".nodes-select2");
    usersSelect2Init(".users-select2");

    initDateRangePicker('#date_start');
    initDateRangePicker('#date_expired');
    initAssetTreeModel('#id_assets');
}).on("change", "input[name=actions]", function (e) {
    let idAction = e.target.getAttribute("id");
    if (idAction === idActionAll){
        $.each(elementActionAllChildren, function(i, elementAction){
            elementAction.checked = elementActionAll.checked
        });
        return;
    }
    else if (idAction === idActionUpDownload){
        $.each(elementActionUpDownloadChildren, function(i, elementAction){
            elementAction.checked = elementActionUpDownload.checked
        });
    }
    else if ([idActionUpload, idActionDownload].indexOf(idAction) !== -1){
        elementActionUpDownload.checked = elementActionUpload.checked && elementActionDownload.checked
    }
    let checked = true;
    $.each(elementActionAllChildren, function(i, elementAction){
        checked &= elementAction.checked;
    });
    elementActionAll.checked = checked;
})
.on("submit", "form", function (evt) {
    evt.preventDefault();
    var theUrl = '{% url 'api-perms:asset-permission-list' %}';
    var method = "POST";
    {% if api_action == "update" %}
        theUrl = '{% url 'api-perms:asset-permission-detail' pk=object.id %}';
        method = "PUT";
    {% endif %}
    var redirect_to = '{% url "perms:asset-permission-list" %}';
    var form = $("form");
    var data = form.serializeObject();
    objectAttrsIsList(data, ['users', 'user_groups', 'system_users', 'nodes', 'assets', 'actions']);
    objectAttrsIsDatetime(data, ['date_start', 'date_expired']);
    objectAttrsIsBool(data, ['is_active']);
    var props = {
        url: theUrl,
        data: data,
        method: method,
        form: form,
        redirect_to: redirect_to
    };
    formSubmit(props);
})
</script>
{% endblock %}
